<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 */

if ( ! function_exists('json_encode_apos'))
{
	function json_encode_apos($a = FALSE)
	{
		if (is_null($a))
			return 'null';
		
		if ($a === FALSE)
			return 'false';
		
		if ($a === TRUE)
			return 'true';
		
		if (is_scalar($a))
		{
			if (is_float($a))
			{
				// Always use "." for floats.
				return floatval(str_replace(",", ".", strval($a)));
			}

			if (is_string($a))
			{
				static $jsonReplaces = array(array("\\", "/", "\n", "	", "\r", "\b", "\f", '"', "'"), array('\\\\', '\\/', '\\n', '\\u0009', '\\r', '\\b', '\\f', '\"', '\\u0027'));
				return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
			}
			else
				return $a;
		}
		$isList = TRUE;
		for ($i = 0, reset($a); $i < count($a); $i++, next($a))
		{
			if (key($a) !== $i)
			{
				$isList = FALSE;
				break;
			}
		}
		$result = array();
		if ($isList)
		{
			foreach ($a as $v) $result[] = json_encode_apos($v);
			return '[' . join(',', $result) . ']';
		}
		else
		{
			foreach ($a as $k => $v)
			{
				$result_item_key = json_encode_apos($k);
				
				if (is_int($result_item_key))
					$result_item_key = '"' . $result_item_key . '"';
				
				$result[] = $result_item_key . ':' . json_encode_apos($v);
			}
			
			return '{' . join(',', $result) . '}';
		}
	}
}
